//
//  CacheManager.m
//  WYProject
//
//  Created by lbh on 2017/11/21.
//  Copyright © 2017年 WeiYeChuangTou. All rights reserved.
//

#import "CacheManager.h"

@implementation CacheManager
+(BOOL)archiveObj:(id)obj namePath:(NSString *)namePath {
    return [NSKeyedArchiver archiveRootObject:obj toFile:[self keyPath:namePath]];
}

+ (id)unarchiveObj:(NSString *)namePath {
    return [NSKeyedUnarchiver unarchiveObjectWithFile:[self keyPath:namePath]];
}

+ (BOOL)removeObj:(NSString *)namePath {
    NSString *keyPath = [self keyPath:namePath];
    NSFileManager *manager = [NSFileManager defaultManager];
    return [manager removeItemAtPath:keyPath error:nil];
}

+(NSString *)keyPath:(NSString *)namePath {
    NSString* dataPath = [kDocPath stringByAppendingPathComponent:[[NSBundle mainBundle] bundleIdentifier]];
    NSFileManager *manager = [NSFileManager defaultManager];
    NSError *error  = nil;
    if (![manager fileExistsAtPath:dataPath]) {
        [manager createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&error];
    }
    NSString *path = [dataPath stringByAppendingPathComponent:namePath];
    
    return path;
}

+ (BOOL)removeAllDocumentDirectory {
    NSFileManager* fileManager=[NSFileManager defaultManager];
    
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES).firstObject;
    
    BOOL blDele= [fileManager removeItemAtPath:path error:nil];
    
    return blDele;
}
///计算缓存数据
+ (NSInteger)getCachesSizeCount {
    
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    NSArray *contants = [manager subpathsAtPath:caches];
    NSInteger totalCount = 0;
    for (NSString *fileName in contants) {
        NSString *path = [caches stringByAppendingPathComponent:fileName];
        if ([manager fileExistsAtPath:path]) {
            NSInteger size = [([manager attributesOfItemAtPath:path error:nil][NSFileSize]) integerValue];
            if ([path rangeOfString:@"tabbarIcon"].location != NSNotFound) {
                /// 网上下载的tabbar图标 不参与计算
            } else {
                totalCount += size;
            }
        }
    }
    return totalCount;
}
///清除缓存
+ (void)cleardDish {
    
    NSFileManager *manager = [NSFileManager defaultManager];
    NSInteger totalCount = 0;
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    NSArray *contents = [manager subpathsAtPath:caches];
    for (NSString *fileName in contents) {
        NSString *path = [caches stringByAppendingPathComponent:fileName];
        if ([manager fileExistsAtPath:path]) {
            NSInteger size = [[manager attributesOfItemAtPath:path error:nil][NSFileSize]intValue];
            totalCount += size;
            if ([path rangeOfString:@"tabbarIcon"].location != NSNotFound) {
                /// 网上下载的tabbar图标 不删除缓存
            } else {
                [manager removeItemAtPath:path error:nil];
            }
        }
    }
}
@end
